﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MusicWorks.Web.Logic.ShoppingService.svc;
using MusicWorks.Web.Logic.Views;
using Rhino.Mocks;
using MusicWorks.Web.Logic.Presenters;
using MusicWorks.Web.Logic.Helpers;

namespace MusicWorks.UnitTests.Web.Logic
{
    [TestClass]
    public class BasketPresenterTests
    {
        [TestMethod]
        public void BasketPresenter_Load_SetsDefaultTotalOnModel()
        {
            var context = new Context();

            context.RaiseLoadEvent();

            Assert.AreEqual(0M, context.View.Model.TotalPrice);
        }

        [TestMethod]
        public void BasketPresenter_Load_SetsDefaultFormattedTotalOnModel()
        {
            var context = new Context();

            context.RaiseLoadEvent();

            Assert.AreEqual("$0.00", context.View.Model.FormattedTotalPrice);
        }

        [TestMethod]
        public void BasketPresenter_Load_SetsDefaultLineItemCollectionOnModel()
        {
            var context = new Context();

            context.RaiseLoadEvent();

            Assert.AreEqual(0, context.View.Model.LineItems.Count());
        }

        [TestMethod]
        public void BasketPresenter_Load_SetsTotalOnModelFromBasket()
        {
            var context = new Context();
            
            var userId = Guid.NewGuid();
            context.Session.UserId = userId;

            context.View.Stub(s => s.BasketName).Return("abc");

            context.Shopping.Stub(s => s.GetBasket(userId, "abc")).Return(new Basket { TotalPrice = 10M });
            context.RaiseLoadEvent();

            Assert.AreEqual(10M, context.View.Model.TotalPrice);
        }

        [TestMethod]
        public void BasketPresenter_Load_SetsFormattedTotalOnModelFromBasket()
        {
            var context = new Context();

            var userId = Guid.NewGuid();
            context.Session.UserId = userId;

            context.View.Stub(s => s.BasketName).Return("abc");

            context.Shopping.Stub(s => s.GetBasket(userId, "abc")).Return(new Basket { TotalPrice = 10M });
            context.RaiseLoadEvent();

            Assert.AreEqual("$10.00", context.View.Model.FormattedTotalPrice);
        }

        class Context
        {
            public IShoppingService Shopping { get; private set; }
            public ISessionValueProvider Session { get; private set; }
            public IBasketView View { get; private set; }
            public BasketPresenter Presenter { get; private set; }

            public Context()
            {
                Shopping = MockRepository.GenerateStub<IShoppingService>();
                Session = MockRepository.GenerateStub<ISessionValueProvider>();
                View = MockRepository.GenerateStub<IBasketView>();
                Presenter = new BasketPresenter(View, Shopping, Session);
            }

            public void RaiseLoadEvent()
            {
                View.Raise(v => v.Load += null, View, EventArgs.Empty);
            }
        }
    }
}
